package com.jzg.sys.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzg.sys.common.rest.BaseRest;
import com.jzg.sys.common.vo.ResultVO;
import com.jzg.sys.dao.entity.SecMenu;
import com.jzg.sys.dao.entity.SecRole;
import com.jzg.sys.exception.BusinessException;
import com.jzg.sys.manager.SecMenuManager;
import com.jzg.sys.manager.SecRoleMenuManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 角色菜单
 * @author:asxx 李志鹏
 * @create:2019-02-28 15:36
 */
@Controller
@RequestMapping(value = "/roleMenuAction")
public class RoleMenuAction extends BaseRest {
    @Autowired
    private SecRoleMenuManager roleMenuManager;
    @Resource
    private SecMenuManager menuManager;

    @RequestMapping(value = "/findRolePage")
    @ResponseBody
    public IPage<SecRole> findRolePage(SecRole secRole, Page<SecRole> page) {
        return roleMenuManager.findPage(secRole, page);
    }

    /**
     * @param sysCode
     * @return
     * @Title: findTree
     * @Description: 同步查询所有菜单树
     * @Author: lzp 李志鵬
     * @Date: 2019/3/7
     */
    @RequestMapping(value = "/findMenuAllTree")
    @ResponseBody
    public List<SecMenu> findMenuAllTree(String sysCode) {
        List<SecMenu> list = menuManager.findMenuAllTree(sysCode);
        return list;
    }

    /**
     * @param roleId
     * @return
     * @Title: findCheckedKeys
     * @Description: 查询角色已拥有的菜单id
     * @Author: lzp 李志鵬
     * @Date: 2019/3/7
     */
    @RequestMapping(value = "/findCheckedKeys")
    @ResponseBody
    public List<String> findCheckedKeys(String roleId) {
        return roleMenuManager.findCheckedKeys(roleId);
    }


    /**
    * @Title: saveRoleMenu
    * @Description: 保存角色菜单并授权
    * @param secMenu
    * @return
    * @Author: lzp 李志鵬
    * @Date: 2019/3/7
    */
    @RequestMapping(value = "/saveRoleMenu")
    @ResponseBody
    public ResultVO saveRoleMenu(SecMenu secMenu) {
        try {
            roleMenuManager.saveRoleMenu(secMenu);
            return new ResultVO();
        } catch (BusinessException be) {
            be.printStackTrace();
            return new ResultVO(false, be.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ResultVO(false, "保存出错");
        }
    }

}
